// https://leetcode-cn.com/problems/product-of-array-except-self/
// 238. Product of Array Except Self
// Given an integer array nums, return an array answer such that answer[i] is
// equal to the product of all the elements of nums except nums[i]
// You must write an algorithm that runs in O(n) time and without using the
// division operation
function productExceptSelf(nums: number[]): number[] {
    const n = nums.length;
    const answer = new Array(n).fill(1);

    for (let i = 1; i < n; i++) {
        answer[i] = nums[i - 1] * answer[i - 1];
    }

    let R = 1;
    for (let i = n - 1; i >= 0; i--) {
        answer[i] = answer[i] * R;
        R = R * nums[i];
    }

    return answer;
}
